package com.djzhou.gmms.service.email.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.email.EmailService;
import com.djzhou.gmms.model.announcement.TbAnnouncement;
import com.djzhou.gmms.model.email.TbEmail;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.message.TbMessage;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.util.DateUtil;

public class EmailServiceImpl extends BaseServiceImpl implements EmailService{

	@Override
	public void add(TbEmail t) {
		emailDAO.insert(t);
	}

	@Override
	public void delete(Integer id) {
		emailDAO.delete(TbEmail.class, id);
		
	}

	@Override
	public TbEmail queryById(Integer id) {
		return emailDAO.queryByID(TbEmail.class, id);
	}

	@Override
	public List<TbEmail> queryList(HashMap<String, Object> hashMap) {
		return emailDAO.query("from TbEmail where deleteFlag=0");
	}

	@Override
	public PageBean<TbEmail> queryPageBean(
			HashMap<String, Object> hashMap, int pageSize, int currentPage) {
		String hql="from TbEmail email where email.deleteFlag=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("startDate")!=null){
				sb.append(" and email.createTime>='").append(hashMap.get("startDate").toString()+"'");
			}
			if(hashMap.get("endDate")!=null){
				sb.append(" and email.createTime<='").append(DateUtil.turnDateToString(DateUtil.getAfterDate(DateUtil.turnStringToDate(hashMap.get("endDate").toString(), "yyyy-MM-dd"), 1), "yyyy-MM-dd")+"'");
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbEmail t) {
		emailDAO.update(t);
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageBean<TbEmail> queryDelete(Integer userId, int pageSize,
			int currentPage) {
		String hql="from TbEmail email where email.receiveDelete=1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.receiver.userId=").append(userId);
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public PageBean<TbEmail> queryDraft(Integer userId, int pageSize,
			int currentPage) {
		String hql="from TbEmail email where email.sendDelete=0 and email.draftFlag=1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.sender.userId=").append(userId);
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public PageBean<TbEmail> queryReceiver(Integer userId, int pageSize,
			int currentPage) {
		String hql="from TbEmail email where email.receiveDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.receiver.userId=").append(userId);
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public PageBean<TbEmail> querySender(Integer userId, int pageSize,
			int currentPage) {
		String hql="from TbEmail email where email.sendDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.sender.userId=").append(userId);
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public PageBean<TbEmail> queryStar(Integer userId, int pageSize,
			int currentPage) {
		String hql="from TbEmail email where email.receiveDelete=0 and email.emailLevel=1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and (email.receiver.userId=").append(userId);
		sb.append(" or email.sender.userId=").append(userId).append(")");
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		PageBean<TbEmail> pageBean=emailDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public int queryTodaySum(Integer userId) {
		String hql="from TbEmail email where email.receiveDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.receiver.userId=").append(userId);
		sb.append(" and email.sendDate>=?");
		sb.append(" and email.sendDate<=?");
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		List<TbEmail> list=emailDAO.query(hql,new Object[]{DateUtil.turnStringToDate(DateUtil.getNowTime("yyyy-MM-dd"),"yyyy-MM-dd"),DateUtil.getAfterDate(DateUtil.turnStringToDate(DateUtil.getNowTime("yyyy-MM-dd"), "yyyy-MM-dd"), 1)});
		return list.size();
	}

	@Override
	public int queryUnreadSum(Integer userId) {
		String hql="from TbEmail email where email.receiveDelete=0 and email.receiver.userId=? and email.emailState=0 order by email.sendDate desc";
		List<TbEmail> list=emailDAO.query(hql,userId);
		return list.size();
	}

	@Override
	public void deleteReceiver(Integer emailId) {
		TbEmail email=emailDAO.queryByID(TbEmail.class, emailId);
		email.setReceiveDelete(1);
		emailDAO.update(email);
	}

	@Override
	public void deleteSender(Integer emailId) {
		TbEmail email=emailDAO.queryByID(TbEmail.class, emailId);
		email.setSendDelete(1);
		emailDAO.update(email);
	}

	@Override
	public void deleteDraft(Integer emailId) {
		emailDAO.delete(TbEmail.class, emailId);
		
	}

	@Override
	public void deleteDelete(Integer emailId) {
		TbEmail email=emailDAO.queryByID(TbEmail.class, emailId);
		email.setReceiveDelete(999);
		emailDAO.update(email);
	}

	@Override
	public int queryReceiverCount(Integer userId) {
		String hql="from TbEmail email where email.receiveDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and email.receiver.userId=").append(userId);
		sb.append(" order by email.sendDate desc");
		hql=sb.toString();
		List<TbEmail> emailList=emailDAO.query(hql);
		return emailList!=null && emailList.size()!=0?emailList.size():0;
	}
}
